<?php

namespace app\admin\controller;

use think\Db;
use think\Request;

class Help extends Common
{
    public function category()
    {
        $where = ['sys_channel'=>getenv('APP_CHANNEL')];

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $data = Db::name('help_category')->where($where)->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function addCategory()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');

            if (!$id) {
                $post['sys_channel'] = getenv('APP_CHANNEL');
                $res = Db::name('help_category')->insert($post);
                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $res = Db::name('help_category')->where('id', $id)->update($post);
                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('help_category')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }

        return $data;
    }

    public function delCategory()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('help_category')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }

    public function question()
    {
        $where = ['sys_channel'=>getenv('APP_CHANNEL')];

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $data = Db::name('help_question')->where($where)->order('sort desc, id asc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $dataList = $data->all();

        $cates = db('help_category')->order('sort desc, id asc')->column('id,title', 'id');

        foreach ($dataList as &$value) {
            $value['cate_name'] = $cates[$value['cid']] ?? '';
        }
        $this->assign('data', $dataList);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function addQuestion()
    {
        $category = Db::name('help_category')->where(['sys_channel'=>['in', [0, getenv('APP_CHANNEL')]]])->select();
        $this->assign('category', get_tree_option1($category, 0));

        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');
            $post['content'] = Request::instance()->post('content','',null);

            if (!isset($post['content']) || !$post['content']) {
                return ['err' => '内容不能为空'];
            }

            if (!$id) {
                $post['sys_channel'] = getenv('APP_CHANNEL');
                $res = Db::name('help_question')->insert($post);
                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $res = Db::name('help_question')->where('id', $id)->update($post);
                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('help_question')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }

        return $data;
    }

    public function delQuestion()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('help_question')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
}
